<?php

namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Department;
use AuctionCore\Account\FrontUser;
use AuctionCore\AntNest\UserCenter;
use AuctionCore\System\AuctionException;
use AuctionCore\CarDealer\DealerUserList;
use AuctionCore\Common\Page;
use AuctionCore\CarDealer\Dealer;
use AuctionCore\CarDealer\TakeMan;
use AuctionCore\Other\Address;
use AuctionCore\System\SystemLog;
use AuctionCore\CarDealer\DealerList;
use AuctionCore\AuctionOrder\AuOrderList;
use AuctionCore\Account\Back\Employee;
use AuctionCore\AntNest\Sms;


/**
 * 车商相关
 */
class CarDealerController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $branch_id = $this->user_info['branch_id'];

        //分公司列表
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);
        $this->assign('branch_id', $branch_id);
    }

    /**
     * 车商列表
     */
    public function user_list()
    {
        $p = I('request.p/d', 1);
        $p = $p < 2 ? 1 : $p;
        $limit = 10;
        //权限验证
        $auth = Employee::check_emp_auth("CheShangZhangHaoChaKanSuoYou", $this->user_info['emp_id']);
        $this->assign('auth', $auth);
        if (!$auth) {
            $params['manager_id'] = $this->user_info['emp_id'];
        } else {
            $manager_noarr = [
                [
                    'emp_id' => '-1',
                    'real_name' => '未分配'
                ]
            ];
            $gid = C("ACCOUNT_MANAGER.0");
            $manager_list = Employee::emp_branch_group($this->user_info['branch_id'], $gid);
            $manager_list = array_merge($manager_noarr, $manager_list);
            $this->assign('manager_list', $manager_list);
            $params['manager_id'] = I("get.manager_id/d", 0);
        }
        //搜索条件
        $params['auth'] = $auth;
        $params['dealer_id'] = I('get.dealer_id', '', 'trim');
        $params['user_name'] = I('get.user_name', '', 'trim');
        $params['contact_person'] = I('get.contact_person', '', 'trim');
        $params['contact_mobile'] = I('get.contact_mobile', '', 'trim');
        $params['business_city'] = I('get.business_city/d', 0, 'trim');
        $params['bc_id'] = I('get.bc_id/d', -1);
        $params['auth_type'] = I('get.auth_type/d', -1);
        $params['level'] = I('get.dealer_level/d', -1);
        $params['start_time'] = I('get.start_time');
        $params['end_time'] = I('get.end_time');
        $jinyong_status = I('get.jinyong_status');
        if ($jinyong_status != '') {
            $jinyong_status = intval($jinyong_status);
        }
        $params['status'] = $jinyong_status;

        $branch_id = $this->user_info['branch_id'];
        if ($this->user_info['branch_id'] > 0) {
            $params['branch_id'] = $branch_id;
        }
        $city_list = Address::get_apply_city_with_branch();
        $this->assign('city_list', $city_list);

        $du = new DealerUserList();
        $result = $du->dealerList($p, $limit, $params);
        $total = empty($result['total']) ? 0 : $result['total'];
        $list = empty($result['list']) ?: $result['list'];
        $user_list = empty($result['user_list']) ?: $result['user_list'];
        $edit_auth = Employee::check_emp_auth("CheShangZhangHaoBianJiSuoYou", $this->user_info['emp_id']);
        $this->assign("edit_auth", $edit_auth);
        //开通车商
        $open_auth = Employee::check_emp_auth("KaiTongCheShangZhangHao", $this->user_info['emp_id']);
        $this->assign('open_auth', $open_auth);
        //模拟登录
        $MoNiDengLu = Employee::check_emp_auth("MoNiDengLu", $this->user_info['emp_id']);
        $this->assign('MoNiDengLu', $MoNiDengLu);
        $page = new Page($total, $limit, $params);
        $page_data = $page->page_data();
        $this->assign('list', $list);
        $this->assign('user_list', $user_list);
        $this->assign('params', $params);
        $this->assign('p', $p);
        $this->assign('page_data', $page_data);
        $this->display();
    }

    //添加车商
    public function add_dealer()
    {
        $man_list = \AuctionCore\Account\Back\Employee::get_branch_emp('388');
        $city_list = Address::get_apply_city_with_branch();
        $branch_list = Department::getBranchCompanyList();
        $add_auth = Employee::check_emp_auth("CheShangZhangHaoBianJiSuoYou", $this->user_info['emp_id']);
        $this->assign('city_list', $city_list);
        $this->assign('branch_list', $branch_list);
        $this->assign('add_auth', $add_auth);
        $this->assign('man_list', $man_list);
        $this->display();
    }

    //保存添加车商
    public function save_add()
    {
        $dealer_phone = I('post.dealer_phone/d', 0, 'trim');
        if (empty($dealer_phone)) {
            $this->helpJsonResult('400', '手机号不能为空');
        }
        if (!validatePhone($dealer_phone)) {
            $this->helpJsonResult('400', '请输入正确的手机号');
        }
        $business_city = I('post.business_city/d', 0, 'trim');
        if ($business_city < 1) {
            $this->helpJsonResult('400', '经营城市不能为空');
        }
        $depart_id = Address::get_branch_company_by_city_code($business_city);
        $branch_company = $depart_id;
        $man_id = I('post.man_id/d', 0, 'trim');
        $dealer = array();
        $user = array();
        $dealer['depart_id'] = $branch_company;
        //$dealer['contact_mobile'] = $dealer_phone;
        $dealer['business_city'] = $business_city;
        $dealer['man_id'] = $man_id;

        $user['account_type'] = 2;//车商帐号类型
        $user['mobile'] = $dealer_phone;
        $password = generateCode(6);
        $user['pwd'] = md5($password);
        $user['emp_id'] = $this->user_info['emp_id'];
        $user['emp_ip'] = get_client_ip();

        //新增车商用户
        $res = Dealer::create_dealer($dealer, $user);
        if ($res === false) {
            $this->helpJsonResult(500, "添加失败,服务器错误");
        }
        $Sms = new Sms();
        $Sms->notify(22, $dealer_phone, ['pwd' => $password]);

        SystemLog::add(array(
            'emp_id' => $this->user_info['emp_id'],
            'action' => "新增车商" . $user['user_name'] . " - " . $res
        ));
        $this->helpJsonResult(200, "添加成功");
    }

    /**
     * 更新用户禁用启用状态
     */
    public function updateUserStatus()
    {
        try {
            $uid = I('post.uid/d', 0, 'trim');
            $dealer_id = I('post.dealer_id/d', 0, 'trim');
            $up_type = I('up_type/d', 0, 'trim');
            $user['account_id'] = $uid;
            $user['emp_id'] = $this->user_info['emp_id'];
            $user['emp_ip'] = get_client_ip();

            $action = '';
            if ($up_type == 1) {
                $res = Dealer::enable_dealer_user($user, $dealer_id);
                if (!$res) {
                    $this->helpJsonResult('500', '操作失败');
                }
                $action = "启用车商:" . $dealer_id;
            } elseif ($up_type == 2) {
                $res = Dealer::disable_dealer_user($user, $dealer_id);
                if (!$res) {
                    $this->helpJsonResult('500', '操作失败');
                }
                $action = "禁用车商:" . $dealer_id;
            } else {
                $this->helpJsonResult(400, "操作非法");
            }

            SystemLog::add(array(
                'emp_id' => $this->user_info['emp_id'],
                'action' => $action,
            ));
            $this->helpJsonResult('200', '操作成功');
        } catch (AuctionException $e) {
            $this->helpJsonResult($e->getCode(), $e->getMessage());
        }
    }

    public function getDealerUserInfo()
    {
        $dealer_id = I('post.dealer_id/d', 0, 'trim');
        if ($dealer_id < 1) {
            $this->helpJsonResult('400', '找不到车商信息');
        }
        $d = new Dealer($dealer_id);
        $detail = $d->baseInfo();
        $dealer = [];
        if ($detail['auth_type'] > 1) {
            $dealer['contact_person'] = $detail['contact_person'];
            $dealer['contact_mobile'] = $detail['contact_mobile'];
        } else {
            $dealer['contact_person'] = $detail['real_name'];
            $dealer['contact_mobile'] = $detail['mobile'];
        }
        $dealer['uid'] = $detail['uid'];
        $this->helpJsonResult('200', '', $dealer);
    }

    /**
     * 重置用户密码
     */
    public function resetUserPwd()
    {
        $dealer_id = I('post.dealer_id/d', 0, 'trim');
        $dealer = new Dealer($dealer_id);
        $dealer_info = $dealer->baseInfo();
        $user['account_id'] = $dealer_info['uid'];
        $password = generateCode(6);
        $user['pwd'] = md5($password);
        $user['emp_id'] = $this->user_info['emp_id'];
        $user['emp_ip'] = get_client_ip();

        $res = Dealer::reset_pwd($user);

        if (!$res) {
            $this->helpJsonResult('400', '重置密码失败');
        }
        $Sms = new Sms();
        $Sms->notify(23, $dealer_info['mobile'], ['pwd' => $password]);

        SystemLog::add(
            [
                'emp_id' => $this->user_info['emp_id'],
                'action' => "重置车商密码" . $dealer_info['user_name'] . " - " . $res
            ]
        );

        $this->helpJsonResult('200', '重置密码成功');
    }

    //更改车商类型是否自收车商
    public function changeDealerType()
    {
        $dealer_id = I('post.dealer_id/d', 0);
        $dealer_type = I('post.dealer_type/d', 0);
        $dealer = new Dealer($dealer_id);
        $data['dealer_type'] = $dealer_type;
        $res = $dealer->updateDealer($data);
        if ($res === false) {
            $this->helpJsonResult('400', '操作失败');
        }
        $this->helpJsonResult('200', '操作成功');
    }

    public function edit()
    {
        $dealer_id = I('get.dealer_id');
        $p = I("get.p"); //修改后跳转到当前页面-xmh.2015-12-15
        $d = new Dealer($dealer_id);
        $detail = $d->baseInfo();
        if (empty($detail)) {
            $this->error('未找到车商信息');
        }
        //dump($detail);exit;
        $t_man = new TakeMan($dealer_id);
        $rows = $t_man->get_list(1, ' and is_contact_person = 1');
        if ($rows && count($rows) > 0) {
            $detail['idcard'] = $rows[0]['idcard'];
            $detail['sex'] = $rows[0]['sex'];
        }
        $DEALER_LEVEL = array_splice(C('DEALER_LEVEL'), 1);
        //权限验证
        $auth = Employee::check_emp_auth("CheShangZhangHaoBianJiSuoYou", $this->user_info['emp_id']);
        $this->assign('auth', $auth);
        $man_list = \AuctionCore\Account\Back\Employee::get_branch_emp('388');
        $city_list = Address::get_apply_city_with_branch();
        $branch_list = Department::getBranchCompanyList();
        if ($detail['manager_id'] > 0) {
            $depart_id = Employee::get_emp_branch_id($detail['manager_id']);
            $branch_id = $depart_id;
        } else {
            $branch_id = -1;
        }
        $this->assign('city_list', $city_list);
        $this->assign('branch_list', $branch_list);
        $this->assign('branch_id', $branch_id);
        $this->assign("p", $p);
        $this->assign('dealer', $detail);
        $this->assign('SEX', C('SEX'));
        $this->assign('man_list', $man_list);
        $this->assign('DEALER_LEVEL', $DEALER_LEVEL);
        $this->display();
    }

    //保存修改
    public function saveEdit()
    {
        $dealer = array();
        $user = array();
        $dealer_id = I('post.dealer_id/d', 0, 'trim');
        if ($dealer_id < 1) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $dealer['dealer_id'] = $dealer_id;
        $dealer_obj = new Dealer($dealer_id);
        $dealer_info = $dealer_obj->baseInfo();

        $dealer_phone = I('post.dealer_phone/d', '', 'trim');
        if (empty($dealer_phone)) {
            $this->helpJsonResult(400, "手机号不能为空");
        }
        //$dealer['contact_mobile'] = $dealer_phone;
        $user['mobile'] = $dealer_phone;
        //$find_d = M('car_dealer')->where(['contact_mobile' => $dealer['contact_mobile']])->getField('dealer_id');
        $uc = new UserCenter();
        $result = $uc->info_bymobile($dealer_phone, 2);
        if (!empty($result)) {
            $find_d = Dealer::uid_to_dealerid($result['account_id']);
            if (!empty($find_d) && $find_d != $dealer_id) {
                $this->helpJsonResult(400, "手机号已存在");
            }
        }
        /*if (!empty($find_d) && $find_d != $dealer_id) {
            $this->helpJsonResult(400, "手机号已存在");
        }*/
        //经营城市
        $dealer['business_city'] = I('post.business_city/d', 0, 'trim');
        if ($dealer['business_city'] < 1) {
            $this->helpJsonResult(400, "请选择经营城市");
        }
        //根据经营城市获得所属分公司
        $depart_id = Address::get_branch_company_by_city_code($dealer['business_city']);
        //分公司
        $dealer['depart_id'] = $depart_id;
        //客户经理
        $manager_id = I('post.manager_id/d', 0, 'trim');
        if ($manager_id < 1) {
            $this->helpJsonResult(400, "请选择客户经理");
        }
        //车商等级
        if ($dealer_info['auth_type'] > 0) {
            $dealer['level'] = I('post.dealer_level/d', 0, 'trim');
        }
        //用户信息
        $user['account_id'] = $dealer_info['uid'];
        $user['emp_id'] = $this->user_info['emp_id'];
        $user['emp_ip'] = get_client_ip();
        //车商信息
        $res = $dealer_obj->update_dealer($dealer, $user, $manager_id);
        if ($res === false) {
            $this->helpJsonResult(500, "编辑车商信息失败");
        }
        SystemLog::add(array(
            'emp_id' => $this->user_info['emp_id'],
            'action' => "编辑车商" . $dealer_id . "manager_id:" . $manager_id . json_encode($user) . " - " . json_encode($dealer)
        ));
        $this->helpJsonResult(200, "成功");
    }

    //查看
    public function dealer_info()
    {
        $dealer_id = I('get.dealer_id/d');
        if ($dealer_id < 1) {
            $this->helpJsRedirect("车商信息错误");
        }
        $d = new Dealer($dealer_id);
        $detail = $d->baseInfo();
        $addr = new Address();
        $city = $addr->get_name_by_code($detail['city']);
        $address = $addr->get_full_name($detail['area']) . $detail['address'];
        $detail['address'] = $address;
        $detail['city_name'] = $city;
        $depart = new \AuctionCore\Account\Back\Department($detail['depart_id']);
        $depart_info = $depart->getDepartById($detail['depart_id']);
        $branch_company_info = $depart::getDepartById($depart_info['branch_id']);
        $branch_company = $branch_company_info['name'];
        $detail['branch_company'] = $branch_company;
        //获取提车人信息
        $tm = new TakeMan($dealer_id);
        $man_list = $tm->get_list();
        $this->assign("dealer", $detail);
        $this->assign("man_list", $man_list);
        $this->display();
    }



    /**********************************************车商统计******************************************/
    //车商统计
    public function dealer_statistics()
    {
        $emp_id = $this->user_info['emp_id'];
        //判断当前用户是不是客户经理
        $manager = Dealer::is_manager(C('ACCOUNT_MANAGER.0'), $emp_id);
        // //判断当前用户是不是客户经理主管
        // $manager_all = Dealer::is_manager(C('ACCOUNT_MANAGER_ALL.0'),$emp_id);
        //查看权限
        $rules = "CheShangGuanLiChaKanSuoYou";
        $e = new Employee($this->user_info['username']);
        $auth = $e->check_auth($rules);
        if (!$manager || $auth) {
            $manager_noarr = [
                [
                    'emp_id' => '-1',
                    'real_name' => '未分配'
                ]
            ];
            $manager = \AuctionCore\Account\Back\Employee::get_branch_emp(C('ACCOUNT_MANAGER.0'), $this->user_info['branch_id']);
            $manager = array_merge($manager_noarr, $manager);
        }
        $this->assign('manager', $manager);
        $this->display();
    }

    public function get_dealer_statistics_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['start_time'] = I("get.start_time");
        $params['end_time'] = I("get.end_time");
        $params['dealer_start_time'] = I("get.dealer_start_time");
        $params['dealer_end_time'] = I("get.dealer_end_time");
//        $params['dealer_end_time'] = I("get.dealer_end_time") ? I("get.dealer_end_time") : date('Y-m-d', strtotime('-1 day'));
        $params['account_type'] = I("get.account_type");
        $params['order_mode'] = I("get.order_mode");
        $params['account_manage'] = I("get.account_manage");
        $params['keyword'] = trim(I('get.search_key'));
        $branch_id = $this->user_info['branch_id'];
        if ($this->user_info['branch_id'] > 0) {
            $params['branch_id'] = $branch_id;
        }
        $emp_id = $this->user_info['emp_id'];
        //判断当前用户是不是客户经理
        $manager = Dealer::is_manager(C('ACCOUNT_MANAGER.0'), $emp_id);
        // $manager_all = Dealer::is_manager(C('ACCOUNT_MANAGER_ALL.0'),$emp_id);
        $rules = "CheShangGuanLiChaKanSuoYou";
        $e = new Employee($this->user_info['username']);
        $auth = $e->check_auth($rules);
        if ($manager && !$auth) {
            $params['emp_id'] = $emp_id;
        }
        // }else{
        //     $manager = \AuctionCore\Account\Back\Employee::get_branch_emp(C('ACCOUNT_MANAGER.0'),$this->user_info['branch_id']);
        //     foreach ($manager as $k => $v) {
        //         $new_emp_id[] = $v['emp_id'];
        //     }
        // }
        //获取list
        $ol = new DealerList();
        $result = $ol->get_dealer_statistics_List($page, $size, $params);
        $list = empty($result['list']) ? array() : $result['list'];
        //分页
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();// 分页显示输出

        $this->assign('page', $show);// 赋值分页输出
        $this->assign('list', $list);
        $this->assign("params", $params);
        $this->assign('p', $page);
        $this->display();
    }

    //成交拍品管理
    public function dealerDeal()
    {
        $dealer_id = I('get.dealer_id');
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $st = I('get.st');
        $et = I('get.et');
        if ($et > date('Y-m-d', strtotime('-1 day'))) {
            $et = date('Y-m-d', strtotime('-1 day'));
        }
        $emp_id = $this->user_info['emp_id'];
        //判断当前用户是不是客户经理
        $manager = Dealer::is_manager(C('ACCOUNT_MANAGER.0'), $emp_id);
        if (!$manager) {
            $manager = \AuctionCore\Account\Back\Employee::get_branch_emp(C('ACCOUNT_MANAGER.0'), $this->user_info['branch_id']);
        }
        $this->assign('st', $st);
        $this->assign('et', $et);
        $this->assign('dealer_id', $dealer_id);
        $this->assign('manager', $manager);
        //渠道
        $pfrom = M("come_from")->where("pid=-2 AND is_delete=0")->select();
        $branch_id = $this->user_info['branch_id'];
        //分公司列表
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);
        $this->assign('branch_id', $branch_id);
        $comefrom_list = $this->get_comefrom_list();
        //城市对应的4s店
        $bc_city = $this->bc_city;
        $city_fours_list = array();
        foreach ($bc_city as $kb => $vb) {
            $city_fours = M("come_from")->alias('a')->field('a.from_id,a.from_name')
                ->join(C('DB_PREFIX') . 'fours as f ON a.from_id = f.channel_id')
                ->where(array('f.city' => $kb))
                ->order('from_id ASC,pid DESC')
                ->select();
            $city_fours_list[$kb] = $city_fours;
        }
        $this->assign('pfrom', $pfrom);
        $this->assign('comefrom_list', $comefrom_list);
        $this->assign('city_fours_list', $city_fours_list);
        //排放标准
        $emission = C('EMISSION');
        $this->assign('emission', $emission);
        $this->display();
    }

    public function get_dealerdeal_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $dealer_id = I('get.dealer_id');
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $params['dealer_id'] = $dealer_id;
        $params['start_time'] = I("get.start_time");
        $params['end_time'] = I("get.end_time");
        $params['bid_start_time'] = I("get.bid_start_time");
        $params['bid_end_time'] = I("get.bid_end_time");
        $params['brand_id'] = I("get.brand_id");
        $params['series_id'] = I("get.series_id");
        $params['model_id'] = I("get.model_id");
        $params['emission'] = I("get.emission");
        $params['order_no'] = I("get.order_no"); //拍单号
        $params['comefrom'] = I("get.comefrom");
        //获取list
        $aol = new AuOrderList();
        $result = $aol->get_dealerdeal_list($page, $size, $params);
        // echo M()->getlastsql();
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();  // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign("car_status", $params['car_status']);
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $show_from = get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        $this->display();
    }

    //投标拍品管理
    public function dealerBid()
    {
        $dealer_id = I('get.dealer_id');
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $st = I('get.st');
        $et = I('get.et');
        if ($et > date('Y-m-d', strtotime('-1 day'))) {
            $et = date('Y-m-d', strtotime('-1 day'));
        }
        $emp_id = $this->user_info['emp_id'];
        //判断当前用户是不是客户经理
        $manager = Dealer::is_manager(C('ACCOUNT_MANAGER.0'), $emp_id);
        if (!$manager) {
            $manager = \AuctionCore\Account\Back\Employee::get_branch_emp(C('ACCOUNT_MANAGER.0'), $this->user_info['branch_id']);
        }
        $this->assign('st', $st);
        $this->assign('et', $et);
        $this->assign('dealer_id', $dealer_id);
        $this->assign('manager', $manager);
        //渠道
        $pfrom = M("come_from")->where("pid=-2 AND is_delete=0")->select();
        $branch_id = $this->user_info['branch_id'];
        //分公司列表
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);
        $this->assign('branch_id', $branch_id);
        $comefrom_list = $this->get_comefrom_list();
        //城市对应的4s店
        $bc_city = $this->bc_city;
        $city_fours_list = array();
        foreach ($bc_city as $kb => $vb) {
            $city_fours = M("come_from")->alias('a')->field('a.from_id,a.from_name')
                ->join(C('DB_PREFIX') . 'fours as f ON a.from_id = f.channel_id')
                ->where(array('f.city' => $kb))
                ->order('from_id ASC,pid DESC')
                ->select();
            $city_fours_list[$kb] = $city_fours;
        }
        $this->assign('pfrom', $pfrom);
        $this->assign('comefrom_list', $comefrom_list);
        $this->assign('city_fours_list', $city_fours_list);
        //排放标准
        $emission = C('EMISSION');
        $this->assign('emission', $emission);
        //拍单状态
        $order_mode = C('NEW_ORDER_STATUS');
        $order_status = array();
        foreach ($order_mode as $key => $value) {
            if ($key != 1 && $key != 2) {
                $order_status[$key] = $value;
            }
        }
        $this->assign('order_mode', $order_status);
        $this->display();
    }

    public function get_dealerbid_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $dealer_id = I('get.dealer_id');
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $params['dealer_id'] = $dealer_id;
        $params['start_time'] = I("get.start_time");
        $params['end_time'] = I("get.end_time");
        // $params['bid_start_time'] = I("get.bid_start_time");
        // $params['bid_end_time'] = I("get.bid_end_time");
        $params['brand_id'] = I("get.brand_id");
        $params['series_id'] = I("get.series_id");
        $params['model_id'] = I("get.model_id");
        $params['emission'] = I("get.emission");
        $params['order_no'] = I("get.order_no"); //拍单号
        $params['comefrom'] = I("get.comefrom");
        $params['order_mode'] = I("get.order_mode");
        //获取list
        $aol = new AuOrderList();
        $result = $aol->get_dealerbid_list($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();  // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $show_from = get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        $this->display();
    }

    //竞标拍品管理
    public function dealerBidding()
    {
        $dealer_id = I('get.dealer_id');
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $st = I('get.st');
        $et = I('get.et');
        if ($et > date('Y-m-d', strtotime('-1 day'))) {
            $et = date('Y-m-d', strtotime('-1 day'));
        }
        $emp_id = $this->user_info['emp_id'];
        //判断当前用户是不是客户经理
        $manager = Dealer::is_manager(C('ACCOUNT_MANAGER.0'), $emp_id);
        if (!$manager) {
            $manager = \AuctionCore\Account\Back\Employee::get_branch_emp(C('ACCOUNT_MANAGER.0'), $this->user_info['branch_id']);
        }
        $this->assign('st', $st);
        $this->assign('et', $et);
        $this->assign('dealer_id', $dealer_id);
        $this->assign('manager', $manager);
        //渠道
        $pfrom = M("come_from")->where("pid=-2 AND is_delete=0")->select();
        $branch_id = $this->user_info['branch_id'];
        //分公司列表
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);
        $this->assign('branch_id', $branch_id);
        $comefrom_list = $this->get_comefrom_list();
        //城市对应的4s店
        $bc_city = $this->bc_city;
        $city_fours_list = array();
        foreach ($bc_city as $kb => $vb) {
            $city_fours = M("come_from")->alias('a')->field('a.from_id,a.from_name')
                ->join(C('DB_PREFIX') . 'fours as f ON a.from_id = f.channel_id')
                ->where(array('f.city' => $kb))
                ->order('from_id ASC,pid DESC')
                ->select();
            $city_fours_list[$kb] = $city_fours;
        }
        $this->assign('pfrom', $pfrom);
        $this->assign('comefrom_list', $comefrom_list);
        $this->assign('city_fours_list', $city_fours_list);
        //排放标准
        $emission = C('EMISSION');
        $this->assign('emission', $emission);
        //拍单状态
        $order_mode = C('NEW_ORDER_STATUS');
        $order_status = array();
        foreach ($order_mode as $key => $value) {
            if ($key != 1 && $key != 2) {
                $order_status[$key] = $value;
            }
        }
        $this->assign('order_mode', $order_status);
        $this->display();
    }

    public function get_dealerbidding_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $dealer_id = I('get.dealer_id');
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $params['dealer_id'] = $dealer_id;
        $params['start_time'] = I("get.start_time");
        $params['end_time'] = I("get.end_time");
        $params['bid_start_time'] = I("get.bid_start_time");
        $params['bid_end_time'] = I("get.bid_end_time");
        $params['brand_id'] = I("get.brand_id");
        $params['series_id'] = I("get.series_id");
        $params['model_id'] = I("get.model_id");
        $params['emission'] = I("get.emission");
        $params['order_no'] = I("get.order_no"); //拍单号
        $params['comefrom'] = I("get.comefrom");
        $params['order_mode'] = I("get.order_mode");
        //获取list
        $aol = new AuOrderList();
        $result = $aol->get_dealerbidding_list($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();  // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $show_from = get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        $this->display();
    }

    //关注拍品管理
    public function dealerFollow()
    {
        $dealer_id = I('get.dealer_id');
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $st = I('get.st');
        $et = I('get.et');
        if ($et > date('Y-m-d', strtotime('-1 day'))) {
            $et = date('Y-m-d', strtotime('-1 day'));
        }
        $emp_id = $this->user_info['emp_id'];
        //判断当前用户是不是客户经理
        $manager = Dealer::is_manager(C('ACCOUNT_MANAGER.0'), $emp_id);
        if (!$manager) {
            $manager = \AuctionCore\Account\Back\Employee::get_branch_emp(C('ACCOUNT_MANAGER.0'), $this->user_info['branch_id']);
        }
        $this->assign('st', $st);
        $this->assign('et', $et);
        $this->assign('dealer_id', $dealer_id);
        $this->assign('manager', $manager);
        //渠道
        $pfrom = M("come_from")->where("pid=-2 AND is_delete=0")->select();
        $branch_id = $this->user_info['branch_id'];
        //分公司列表
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);
        $this->assign('branch_id', $branch_id);
        $comefrom_list = $this->get_comefrom_list();
        //城市对应的4s店
        $bc_city = $this->bc_city;
        $city_fours_list = array();
        foreach ($bc_city as $kb => $vb) {
            $city_fours = M("come_from")->alias('a')->field('a.from_id,a.from_name')
                ->join(C('DB_PREFIX') . 'fours as f ON a.from_id = f.channel_id')
                ->where(array('f.city' => $kb))
                ->order('from_id ASC,pid DESC')
                ->select();
            $city_fours_list[$kb] = $city_fours;
        }
        $this->assign('pfrom', $pfrom);
        $this->assign('comefrom_list', $comefrom_list);
        $this->assign('city_fours_list', $city_fours_list);
        //排放标准
        $emission = C('EMISSION');
        $this->assign('emission', $emission);
        //拍单状态
        $order_mode = C('NEW_ORDER_STATUS');
        $order_status = array();
        foreach ($order_mode as $key => $value) {
            if ($key != 1 && $key != 2) {
                $order_status[$key] = $value;
            }
        }
        $this->assign('order_mode', $order_status);
        $this->display();
    }

    public function get_dealerfollow_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $dealer_id = I('get.dealer_id');
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $params['dealer_id'] = $dealer_id;
        $params['start_time'] = I("get.start_time");
        $params['end_time'] = I("get.end_time");
        $params['bid_start_time'] = I("get.bid_start_time");
        $params['bid_end_time'] = I("get.bid_end_time");
        $params['brand_id'] = I("get.brand_id");
        $params['series_id'] = I("get.series_id");
        $params['model_id'] = I("get.model_id");
        $params['emission'] = I("get.emission");
        $params['order_no'] = I("get.order_no"); //拍单号
        $params['comefrom'] = I("get.comefrom");
        $params['order_mode'] = I("get.order_mode");
        //获取list
        $aol = new AuOrderList();
        $result = $aol->get_dealerfollow_list($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();  // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $show_from = get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        $this->display();
    }

    //车商维保保养记录管理
    public function dealerRepair()
    {
        $dealer_id = I('get.dealer_id/d', 0);
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $st = I('get.st');
        $et = I('get.et');
        if ($et > date('Y-m-d', strtotime('-1 day'))) {
            $et = date('Y-m-d', strtotime('-1 day'));
        }
        $emp_id = $this->user_info['emp_id'];
        //判断当前用户是不是客户经理
        $manager = Dealer::is_manager(C('ACCOUNT_MANAGER.0'), $emp_id);
        if (!$manager) {
            $manager = \AuctionCore\Account\Back\Employee::get_branch_emp(C('ACCOUNT_MANAGER.0'), $this->user_info['branch_id']);
        }
        $this->assign('st', $st);
        $this->assign('et', $et);
        $this->assign('dealer_id', $dealer_id);
        $this->display();
    }

    /**
     * 车商维保保养记录管理列表
     */
    public function get_dealer_repair_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $dealer_id = I('get.dealer_id/d', 0);
        if ($dealer_id < 0) {
            $this->helpJsonResult(400, "车商ID不能为空!");
        }
        $params['dealer_id'] = $dealer_id;
        $params['start_time'] = I("get.start_time");
        $params['end_time'] = I("get.end_time");
        $params['order_status'] = I("get.order_status");
        //获取list
        $rc = new \AuctionCore\CarDealer\RepairCheck();
        $result = $rc->get_dealer_repair_check_list($page, $size, $params);
        // echo M()->getlastsql();
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();  // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign("car_status", $params['car_status']);
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $show_from = get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        $this->display();
    }

    /**
     * 车商签约合同
     */
    public function contract()
    {
        $dealer_id = I('get.id/d', '');
        if (empty($dealer_id)) {
            $this->helpJsonResult(400, "车商信息错误!");
        }
        $dealer = new Dealer($dealer_id);
        $contract = $dealer->get_dealer_contract($dealer_id);
        $this->assign('contract', $contract);
        $this->display();
    }

    /*
     *  @虚拟登录来拍车
     *  @auther: 牛治鑫
     */
    public function logintolpaiche()
    {
        $user_info = session('user_info');
        $emp_id = $user_info['emp_id'];
        $dealer_id = I('get.dealer_id');
        $user_name = I('get.user_name');
        $uid = I('get.uid');
        $redis = new \AuctionCore\Common\PhpRedis(0);

        $arr = [
            'uid'=>$uid,
        ];
        $keyName = getRedisKeyByCode('REDIS_KEY','100003',$arr);

        $redis->set($keyName, $user_name);
        //$redis->set('Moni_login_lpaiche_' . $uid, $user_name);
        //记录操作日志
        $log = array('emp_id' => $emp_id, 'action' => "使用模拟登录进入来拍车账号,车商id:'$dealer_id',账号:'$user_name'");
        \AuctionCore\System\SystemLog::add($log);
        $token = $this->set_login_token($uid);
        redirect('http://www.lpaiche.com/Index/login_token?uid=' . $uid . '&token=' . $token);
    }

    //生成虚拟登录token
    private function set_login_token($uid)
    {
        $i = date('i');
        $salt = 'admin.clcw.com.cn';
        $key = md5($uid . $i . $salt);
        $token = $uid . $key;
        return $token;
    }
}